Skip to main content

Java Functional Interfaces

A functional interface in Java is an interface that contains exactly one abstract method. This restriction allows functional interfaces to be used as the basis for lambda expressions and method references, which are key aspects of Java's functional programming capabilities introduced in Java 8.

Key Characteristics of Functional Interfaces:

  1. Single Abstract Method (SAM): The defining characteristic of a functional interface is that it must have only one abstract method. This does not count any default or static methods, even if they are part of the interface.

  2. Use of @FunctionalInterface Annotation: While not mandatory, it's a good practice to annotate a functional interface with @FunctionalInterface. This annotation helps in two ways: it makes the intent clear to the users of the interface, and it triggers a compile-time error if the interface does not adhere to the rules of a functional interface (i.e., if it has more than one abstract method).

  3. Lambda Expressions: The single abstract method of a functional interface can be implemented using a lambda expression, making the code more concise and readable.

Functional interfaces are a cornerstone of functional programming in Java, enabling techniques like higher-order functions, function composition, and more elegant handling of single-method interfaces with lambda expressions and method references.